﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ObligatorioBD.Logic
{
    class GeneralData
    {
        SqlConnection cnx;
        public GeneralData(DatabaseInterface.Connection cnx)
        {
            this.cnx = cnx.getConnection();
        }
        public List<MerObjects.Mer> GetMerList()
        {
            try
            {
                MerBuilder merBuilder = new MerBuilder(this.cnx);
                List<MerObjects.Mer> merList = new List<MerObjects.Mer>();
                SqlCommand cmd = new SqlCommand("SELECT * FROM Mer", this.cnx);

                MerDataSet ds = new MerDataSet();
                //SqlDataReader dr;
                SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, this.cnx);
                da.Fill(ds.Mer);
                foreach (MerDataSet.MerRow row in ds.Mer.Rows)
                {
                    merList.Add(merBuilder.GetMer(row.MerId));
                }
                return merList;
            }
            catch (Exception ex)
            {
                if (this.cnx.State != System.Data.ConnectionState.Closed)
                {
                    this.cnx.Close();
                }
                throw ex;
            }
        }
    }
}
